package com.shuyuntu.login.controller;

import com.shuyuntu.login.entity.User;
import com.shuyuntu.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.sound.midi.SysexMessage;

/**
 * @program: login
 * @Date: 2018-07-11 16:21
 * @Author: Mr.Deng
 * @Description:
 */


@Controller
@EnableAutoConfiguration
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/")
    @ResponseBody
    String welcome() {
        return "welcome my first spring boot project";
    }

    @RequestMapping("/notVerify")
    @ResponseBody
    String notVerify() {
        return "用户名或密码不正确";
    }

    @RequestMapping("/login1")
    String login(Model model) {
        model.addAttribute("user", new User());
        return "login1";
    }

    //注册
    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    @ResponseBody
    String registerUser(User user, Model model) {

        return userService.registerUser(user);
    }

    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    String userLogin(User user, Model model) {

        boolean verify = userService.verifyUser(user);
        if (verify) {
            model.addAttribute("name", user.getName());
            model.addAttribute("password", user.getPassword());
            return "/view/file";
        } else {
            return "redirect:/notVerify";
        }

    }


}
